﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.ParentForm;

namespace UI.ProductManagement
{
    public partial class FrmPot4a00 : BaseMain
    {
        FrmPot4a00Medit edit = null;
        FrmPot4a00NMedit nedit = null;
        private ToolStripButton btnModify = null;

        public FrmPot4a00(string modulecode, string action)
            : base(modulecode, action)
        {
            InitializeComponent();

            //this.fixedColumn = 2;
        }

        protected override void AddToolButton()
        {
            int index = this.tspTop.Items.IndexOf(this.tspEdit);

            btnModify = base.CreateToolBtn("X", "修改",
                   new EventHandler(delegate
                                        {
                                            DataGridViewRow nRow = GetSelectedRow();
                                            if (null == nRow) return;
                                            OpenEdit("X", "修改促销结束日期",
                                                     new string[]
                                                         {
                                                             Components.Validation.ToString(
                                                                 nRow.Cells["promocode"].Value)
                                                         });
                                        }));
            //增加修改结束时间的按钮
            this.tspTop.Items.Insert(index, btnModify);
        }

        protected override void OnAddClick(ToolStripButton btn)
        {
            nedit = new FrmPot4a00NMedit(
                this.ModuleCode, "A", GetTitleStyle(this.Text, "新增"), null);
            nedit.ShowDialog();
            if (nedit.IsReload) base.BindGrid();
        }

        protected override void OnEditClick(ToolStripButton btn, DataGridViewRow row)
        {
            OpenEdit("M", btn.Text, new string[] { Components.Validation.ToString(row.Cells["promocode"].Value) });
        }

        protected override void OnViewClick(ToolStripButton btn, DataGridViewRow row)
        {
            OpenEdit("V", btn.Text, new string[] { Components.Validation.ToString(row.Cells["promocode"].Value) });
        }

        private void OpenEdit(string action, string btnText, string[] code)
        {
            nedit = new FrmPot4a00NMedit(this.ModuleCode, action, GetTitleStyle(this.Text, btnText), code);

            nedit.ShowDialog();
            if (nedit.IsReload) base.BindGrid();
        }

        protected override void UpdatePager(UI.Common.Pager pager)
        {
            pager.FilterAppName = "a.muser";
        }

        protected override void AfterRowChanged(int rowIndex)
        {
            if (rowIndex < 0 || null == btnModify) return;

            //如果当前时间大于某条促销记录的结束时间，则不让修改结束时间
            DateTime serverDate = Components.Validation.ToDate(this.dgvMain["nowdate", rowIndex]);
            object endate = this.dgvMain["endate", rowIndex].Value;

            this.btnModify.Enabled = false;
            if (Components.Validation.IsDateTime(endate) &&
                Components.Validation.ToDate(endate) >= serverDate)
            {

                this.btnModify.Enabled = true;
            }
        }
    }
}
